<?php
session_start(); 

include "connect.php";

$customer_name 			= $_POST['customer_name'];
$customer_email		   = $_POST['customer_email'];
$customer_postcode		= $_POST['customer_postcode'];
$customer_number 		  = $_POST['customer_number'];
$sell_type 				= $_POST['sell_type'];
$estimate_price 		     = $_POST['estimate_price'];
$saddle_name	          = $_POST['saddle_name'];
$saddle_colour 			= $_POST['saddle_colour'];
$saddle_size 		      = $_POST['saddle_size'];
$customer_message 		 = $_POST['customer_message'];

//Create product ID

mysql_query("INSERT INTO customer_sell_requests

(

request_status,
customer_name,
customer_email,
customer_postcode,
customer_number	  ,
sell_type   ,
estimate_price,
saddle_name,
saddle_colour,
saddle_size,
customer_message

) 

VALUES

(

'New',
'$customer_name',
'$customer_email',
'$customer_postcode',
'$customer_number',
'$sell_type',
'$estimate_price',
'$saddle_name',
'$saddle_colour',
'$saddle_size',
'$customer_message'

) 

"); 

$new_request_id = mysql_insert_id();

//ERROR TESTING

 $ftp_server="ftp.24-7saddles.com";
 $ftp_user_name="z247s504";
 $ftp_user_pass="PxLpCd07";

 // set up basic connection
 $conn_id = ftp_connect($ftp_server);

 // login with username and password
 $login_result = ftp_login($conn_id, $ftp_user_name, $ftp_user_pass); 

////////////////
//Upload image 1
////////////////

//Сheck that we have a file
if((!empty($_FILES["uploaded_file_1"])) && ($_FILES['uploaded_file_1']['error'] == 0)) {
//Check if the file is JPEG image and it's size is less than 350Kb
//$filename = basename($_POST['product_id']."_".$_POST['image_number'].".jpg");
$filename = $new_request_id."1.jpg";
$ext = substr($filename, strrpos($filename, '.') + 1);

//Determine the path to which we want to save this file

	//Live
	//$newname = '/home/z247s504/public_html/images/saddles/'.$filename;

	//Test
	$newname = '/home/z247s504/public_html/sell_request_images/'.$filename;

//Attempt to move the uploaded file to it's new place
if ((move_uploaded_file($_FILES['uploaded_file_1']['tmp_name'],$newname)))
{


//
////Resize the image
//////Limit the image to a maximum size

$orig_image = imagecreatefromjpeg($newname);//Get the uploaded image file name
$image_info = getimagesize($newname); //Get image current size
$width_orig  = $image_info[0]; // current width as found in image file
$height_orig = $image_info[1]; // current height as found in image file
$width = 500; // new image width
$height = 500; // new image height
$width = (($width_orig * $height)/$width_orig); // new image width
$destination_image = imagecreatetruecolor($width, $height);
imagecopyresampled($destination_image, $orig_image, 0, 0, 0, 0, $width, $height, $width_orig, $height_orig);

//If the image being uploaded is larger than 500 x 500
if($width_orig > 500 && $height_orig > 500)
{
// This will just copy the new image over the original at the same filePath.
imagejpeg($destination_image, $newname, 100);
$resizeMessage="Image has been resized to (".$width."px x ".$height."px)";
}

//
////
//////End Image Resize

mysql_query("UPDATE customer_sell_requests SET 

saddle_image_1 = '$filename'

WHERE request_id = $new_request_id");

}
else
{
//echo "Image1 Error";
}
} else

{	
//echo "Iamge 1 Error 2";
}

////////////////
//END Upload image 1
////////////////

////////////////
//Upload image 2
////////////////

//Сheck that we have a file
if((!empty($_FILES["uploaded_file_2"])) && ($_FILES['uploaded_file_2']['error'] == 0)) {
//Check if the file is JPEG image and it's size is less than 350Kb
//$filename = basename($_POST['product_id']."_".$_POST['image_number'].".jpg");
$filename = $new_request_id."2.jpg";
$ext = substr($filename, strrpos($filename, '.') + 1);

//Determine the path to which we want to save this file

	//Live
	//$newname = '/home/z247s504/public_html/images/saddles/'.$filename;

	//Test
	$newname = '/home/z247s504/public_html/uat/sell_request_images/'.$filename;

//Attempt to move the uploaded file to it's new place
if ((move_uploaded_file($_FILES['uploaded_file_2']['tmp_name'],$newname)))
{


//
////Resize the image
//////Limit the image to a maximum size

$orig_image = imagecreatefromjpeg($newname);//Get the uploaded image file name
$image_info = getimagesize($newname); //Get image current size
$width_orig  = $image_info[0]; // current width as found in image file
$height_orig = $image_info[1]; // current height as found in image file
$width = 500; // new image width
$height = 500; // new image height
$width = (($width_orig * $height)/$width_orig); // new image width
$destination_image = imagecreatetruecolor($width, $height);
imagecopyresampled($destination_image, $orig_image, 0, 0, 0, 0, $width, $height, $width_orig, $height_orig);

//If the image being uploaded is larger than 500 x 500
if($width_orig > 500 && $height_orig > 500)
{
// This will just copy the new image over the original at the same filePath.
imagejpeg($destination_image, $newname, 100);
$resizeMessage="Image has been resized to (".$width."px x ".$height."px)";
}

//
////
//////End Image Resize

mysql_query("UPDATE customer_sell_requests SET 

saddle_image_2 = '$filename'

WHERE request_id = $new_request_id");

}
else
{
//$_SESSION['updateMessage'] = "Error";    
//header("Location: webmaster_edit_products.php?Error");
//echo "Image 2 Error";
}
} else

{
//$_SESSION['updateMessage'] = "Error";    
//header("Location: webmaster_edit_products.php?Error"); 	
//echo "Iamge 2 Error 2";
}

////////////////
//END Upload image 2
////////////////

////////////////
//Upload image 3
////////////////

//Сheck that we have a file
if((!empty($_FILES["uploaded_file_3"])) && ($_FILES['uploaded_file_3']['error'] == 0)) {
//Check if the file is JPEG image and it's size is less than 350Kb
//$filename = basename($_POST['product_id']."_".$_POST['image_number'].".jpg");
$filename = $new_request_id."3.jpg";
$ext = substr($filename, strrpos($filename, '.') + 1);

//Determine the path to which we want to save this file

	//Live
	//$newname = '/home/z247s504/public_html/images/saddles/'.$filename;

	//Test
	$newname = '/home/z247s504/public_html/uat/sell_request_images/'.$filename;

//Attempt to move the uploaded file to it's new place
if ((move_uploaded_file($_FILES['uploaded_file_3']['tmp_name'],$newname)))
{


//
////Resize the image
//////Limit the image to a maximum size

$orig_image = imagecreatefromjpeg($newname);//Get the uploaded image file name
$image_info = getimagesize($newname); //Get image current size
$width_orig  = $image_info[0]; // current width as found in image file
$height_orig = $image_info[1]; // current height as found in image file
$width = 500; // new image width
$height = 500; // new image height
$width = (($width_orig * $height)/$width_orig); // new image width
$destination_image = imagecreatetruecolor($width, $height);
imagecopyresampled($destination_image, $orig_image, 0, 0, 0, 0, $width, $height, $width_orig, $height_orig);

//If the image being uploaded is larger than 500 x 500
if($width_orig > 500 && $height_orig > 500)
{
// This will just copy the new image over the original at the same filePath.
imagejpeg($destination_image, $newname, 100);
$resizeMessage="Image has been resized to (".$width."px x ".$height."px)";
}

//
////
//////End Image Resize

mysql_query("UPDATE customer_sell_requests SET 

saddle_image_3 = '$filename'

WHERE request_id = $new_request_id");

}
else
{
//echo "Image 3 Error";
}
} else

{	
//echo "Iamge 3 Error 2";
}

////////////////
//END Upload image 3
////////////////

////////////////
//Upload image 4
////////////////

//Сheck that we have a file
if((!empty($_FILES["uploaded_file_4"])) && ($_FILES['uploaded_file_4']['error'] == 0)) {
//Check if the file is JPEG image and it's size is less than 350Kb
//$filename = basename($_POST['product_id']."_".$_POST['image_number'].".jpg");
$filename = $new_request_id."4.jpg";
$ext = substr($filename, strrpos($filename, '.') + 1);

//Determine the path to which we want to save this file

	//Live
	//$newname = '/home/z247s504/public_html/images/saddles/'.$filename;

	//Test
	$newname = '/home/z247s504/public_html/uat/sell_request_images/'.$filename;

//Attempt to move the uploaded file to it's new place
if ((move_uploaded_file($_FILES['uploaded_file_4']['tmp_name'],$newname)))
{


//
////Resize the image
//////Limit the image to a maximum size

$orig_image = imagecreatefromjpeg($newname);//Get the uploaded image file name
$image_info = getimagesize($newname); //Get image current size
$width_orig  = $image_info[0]; // current width as found in image file
$height_orig = $image_info[1]; // current height as found in image file
$width = 500; // new image width
$height = 500; // new image height
$width = (($width_orig * $height)/$width_orig); // new image width
$destination_image = imagecreatetruecolor($width, $height);
imagecopyresampled($destination_image, $orig_image, 0, 0, 0, 0, $width, $height, $width_orig, $height_orig);

//If the image being uploaded is larger than 500 x 500
if($width_orig > 500 && $height_orig > 500)
{
// This will just copy the new image over the original at the same filePath.
imagejpeg($destination_image, $newname, 100);
$resizeMessage="Image has been resized to (".$width."px x ".$height."px)";
}

//
////
//////End Image Resize

mysql_query("UPDATE customer_sell_requests SET 

saddle_image_4 = '$filename'

WHERE request_id = $new_request_id");

}
else
{
//echo "Image 4 Error";
}
} else

{	
//echo "Iamge 4 Error 2";
}

////////////////
//END Upload image 4
////////////////

header("Location: sell_request_landing_page.php");

?>